package com.baizhi.service.impl;

import com.baizhi.dao.CategoryDao;
import com.baizhi.entity.Category;
import com.baizhi.service.CategoryService;
import com.baizhi.utils.MyBatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class CategoryServiceImpl implements CategoryService {
    @Override
    public List<Category> selectAllCategory() {
        List<Category> categories=new ArrayList<>();
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CategoryDao categoryDao = sqlSession.getMapper(CategoryDao.class);

            categories = categoryDao.selectAllCategory();
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return categories;
    }

    @Override
    public void addCategoryLevels1(Category category) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CategoryDao categoryDao = sqlSession.getMapper(CategoryDao.class);
            categoryDao.addCategoryLevels1(category);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public List<Category> selectCategoryLevels1() {
        List<Category> categories=new ArrayList<>();
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CategoryDao categoryDao = sqlSession.getMapper(CategoryDao.class);
            categories=categoryDao.selectCategoryLevels1();
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return categories;
    }

    @Override
    public List<Category> selectCategoryLevels2() {
        List<Category> categories=new ArrayList<>();
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CategoryDao categoryDao = sqlSession.getMapper(CategoryDao.class);
            categories=categoryDao.selectCategoryLevels2();
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return categories;
    }

    @Override
    public void addCategoryLevels2(Category category) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CategoryDao categoryDao = sqlSession.getMapper(CategoryDao.class);
            categoryDao.addCategoryLevels2(category);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public void deleteCategory(String categoryId) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CategoryDao categoryDao = sqlSession.getMapper(CategoryDao.class);
            categoryDao.deleteCategory(categoryId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public Category selectCategoryByName(String categoryName) {
        Category category=null;
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CategoryDao categoryDao = sqlSession.getMapper(CategoryDao.class);
            category= categoryDao.selectCategoryByName(categoryName);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return category;
    }

    @Override
    public Category selectCategoryById(String categoryId) {
        Category category=null;
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CategoryDao categoryDao = sqlSession.getMapper(CategoryDao.class);
            category= categoryDao.selectCategoryById(categoryId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return category;
    }

    @Override
    public Integer selectCountForLevelsById(Integer levels,String categoryId) {
        Integer count=0;
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CategoryDao categoryDao = sqlSession.getMapper(CategoryDao.class);
            if (levels==1){
                count=categoryDao.selectLevels1ForLevels2CountById(categoryId);
            }else {
                count=categoryDao.selectLevels2ForBookCountById(categoryId);
            }
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return count;
    }


    @Override
    public List<Category> selectCategoryLevels1s() {
        List<Category> categories=new ArrayList<>();
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CategoryDao categoryDao = sqlSession.getMapper(CategoryDao.class);
            categories=categoryDao.selectCategoryLevels1s();
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return categories;
    }

    @Override
    public List<Category> selectCategoryByFirstId(String firstId) {
        List<Category> categories=new ArrayList<>();
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            CategoryDao categoryDao = sqlSession.getMapper(CategoryDao.class);
            categories=categoryDao.selectCategoryByFirstId(firstId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return categories;
    }

}
